package com.xm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.xm.entity.Banners;
import com.xm.entity.Images;
import com.xm.uti.DBManager;
import com.xm.uti.QueryDataResult;

/**
 * 产品图片持久层的展示
 * 
 * @author kh
 *
 */
public class ImageDaoImpl {
	public ArrayList<Images> getImgListBypId(int pid) {
		String sql = "select * from images where Productid= " + pid + " order by id desc";
		System.out.println("执行的sql语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Images> list = new ArrayList<Images>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Images e = new Images();
					e.setId(rs.getInt(1));
					e.setUrl(rs.getString("url"));
					e.setProductid(rs.getInt("Productid"));
					list.add(e);
				}
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}
	
	
	
	/**
	 * 产品详情图片（根据产品id单查相关的详情图片）
	 * 
	 * @author kh
	 *
	 */
	public ArrayList<Images> ProImageList(String pid) {
		String sql = "select * from images where Productid = "+pid;
		System.out.println("执行的sql语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Images> list = new ArrayList<>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Images e = new Images();
					e.setId(rs.getInt(1));
					e.setUrl(rs.getString("url"));
					e.setProductid(rs.getInt("Productid"));
					list.add(e);
				}
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println("查询到的数据是"+list);
		return list;
	}
	

	/**
	 * 产品详情图片（全查方法）
	 * 
	 * @author kh
	 *
	 */
	public ArrayList<Images> getImage() {
		String sql = "select * from images";
		System.out.println("执行的sql语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Images> list = new ArrayList<Images>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Images e = new Images();
					e.setId(rs.getInt(1));
					e.setUrl(rs.getString("url"));
					e.setProductid(rs.getInt("Productid"));
					list.add(e);
				}
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

	/**
	 * 增
	 * 
	 * @param emp
	 * @return
	 */
	public boolean insertImage(Images imges) {
		String sql = "insert into images (url,productid) values ('" + imges.getUrl() + "','"
				+ imges.getProductid() + "')";
		System.out.println("执行的sql语句是" + sql);
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除
	 * 
	 * @param emp
	 * @return
	 */
	public boolean delImage(int id) {
		String sql = "delete from images where id = " + id;
		System.out.println("执行的sql语句是" + sql);
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 修改
	 * 
	 * @param emp
	 * @return
	 */
	public boolean updateImage(Images images) {
		String sql = "update images set url='" + images.getUrl() + "',productid='" + images.getProductid()
				+ "' where id =" + images.getId();
		System.out.println("执行的sql语句是" + sql);
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

}